package com.shinechain.utils;

import com.shinechain.dao.BotAdMapper;
import com.shinechain.dao.BotChatMapper;
import com.shinechain.dao.BotKitManMapper;
import com.shinechain.model.BotAd;
import com.shinechain.model.BotChat;
import com.shinechain.model.BotKitMan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service(value = "patternChat")
public class PatternChat {

/*
*
 */

    /**
     * hehe
     */
    @Autowired
    BotChatMapper botChatMapper;

   @Autowired
   BotAdMapper botAdMapper;

   @Autowired
    BotKitManMapper botKitManMapper;


   // public static String []dirty = {"fk"};
    // public static String []regEx = {"f.{0,5}u.{0,5}c.{0,5}k"};

    public static HashMap<String,String> hashMap = new HashMap<>();

    public static List<String> list = new ArrayList<>();



    public static boolean patternAd(String message){
        boolean flag;
        for(int i=0 ; i<list.size(); i++){

            String regEx = list.get(i);



        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(message);
            if(matcher.find()){
                return true;
            }
        }
        return false;
    }

    public static String patternChat(String message){
        return hashMap.get(message);
    }

    public  void updateMap() {
        System.out.println(botChatMapper==null);

            List<BotChat> newList = botChatMapper.findAll();
            if (!newList.isEmpty()) {
                hashMap.clear();
                for (int i = 0; i < newList.size(); i++) {
                    hashMap.put(newList.get(i).getKeyword(), newList.get(i).getResponse());
                }
            }

    }

    public  void updateList(){
        List<BotAd> newList = botAdMapper.findAll();

      //  System.out.println(newList.get(0).getAd());
        if (!newList.isEmpty()) {
            list.clear();
            for(int i = 0 ; i<newList.size() ; i++){
                String reg = newList.get(i).getAd();
                String regEx = "";
                for(int j=0 ; j<reg.length() ; j++){
                    regEx += "(?i)"+reg.substring(j,j+1)+".{0,5}";
                    System.out.println(reg.substring(j,j+1));
                }
                list.add(regEx);
            }
            System.out.println(list.get(0));

            }
        }

        public int insert(BotKitMan botKitMan){
        return botKitManMapper.insert(botKitMan);
        }
    }

